package org.wattdepot.cli.command;

import java.util.ArrayList;
import java.util.List;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.cli.processor.CommandLineInterface;
import org.wattdepot.client.WattDepotClientException;

/**
 * Creates a list of all power sources on the Oahu grid containing name, parent, and a description
 * of each plant. Adapted from branch ekolu to return a string. This class extends
 * CommandLineInterface to have access to the WattDepotClient and implements WattDepotCliCommand
 * interface to ensure this class does a specific command.
 * 
 * @author Edward Meyer, Bao Huy Ung
 */
public class ListSources extends CommandLineInterface implements WattDepotCliCommand {
  /** Carriage return on this operating system. */
  public String cr = System.getProperty("line.separator");

  /**
   * Constructor for a ListSources object which lists information of all power grids on WattDepot
   * Server.
   */
  public ListSources() {
    // Empty constructor.
  }

  /**
   * Creates a list of all power sources on the Oahu grid containing name, parent, and a description
   * of each plant. Adapted from branch ekolu to return a string.
   * 
   * @param args Is a list of all the arguments entered by the user.
   * @return buff An appended string of all the information about each plant.
   */
  public String doCommand(List<String> args) {

    if (args.size() != 1) {
      return "Error: Command contains extraneous arguments at the end.";
    }

    // Append information of each grid to buff.
    StringBuffer buff = new StringBuffer(20);
    // Used to scan through the names of each plant.
    // Initialize to store names and area of each plant.
    String childName = "";
    String parentName = "";

    // Format variable used to create 3 columns of lengths 20 and 50.
    String format = "%1$-20s %2$-20s %3$-50s" + cr;
    // Append headings
    buff.append(String.format(format, "Name", "Parent", "Description"));

    // Create a list of all plant sources.
    List<Source> serverSources = null;
    try {
      serverSources = client.getSources();
    }
    catch (WattDepotClientException e) {
      return "An error occured while trying to retrieve all sources.\n" + e;
    }
    List<Source> parentSources = new ArrayList<Source>();

    // Create a list of sources that do have subsources, essentially creating a list of parents.
    for (Source source : serverSources) {
      if (source.getSubSources() != null) {
        parentSources.add(source);
      }
    }

    // Iterate through all the sources and append the information to buff.
    for (Source source : serverSources) {
      childName = source.getName();
      // Special case since "SIM_OAHU_GRID" is at the top level of power grid, has no parent.
      if ("SIM_OAHU_GRID".equals(childName)) {
        buff.append(String.format(format, childName, "", source.getDescription()));
        continue;
      }
      // Use the parent list to output the parents of all the children.
      for (Source parent : parentSources) {
        if (parent.getSubSources().toString().matches(".*" + source.getName() + ".*")
            && !parent.getName().equals(source.getName())) {
          parentName = parent.getName();
        }
      }
      buff.append(String.format(format, childName, parentName, source.getDescription()));
    }
    return buff.toString();
  }
}